package me.dz.dreamcleaner.manager;

import java.util.ArrayList;
import static me.dz.dreamcleaner.utils.MsgUtils.*;

public class FlexibleFormula {

    private String statement = "该代码由梦幻都市服主原创制作" +
                    "作者QQ 1098579051";

    /*
       支持() * / + - 的所有运算
        */
    public static double mathematical(String str) {
        try{
            return new FlexibleFormula(str).getResult();
        }catch (Exception i){
            console("&c公式错误,运算失败:&b " + str);
            return 0;
        }
    }
    private String text;

    public FlexibleFormula(String text){
        this.text = text;
    }
    public double getResult(){
        if (text == null || text.length() == 0)return 0;
        try {
            return Double.parseDouble(text);
        }catch (NumberFormatException ignored){}
        text = text.replace(" ","")
                .replace("--","+")
                .replace("-+","-")
                .replace("+-","-");
        if (text.contains("(")) {
            if (!text.contains(")")){
                text = text.replace("(","");
            }else {
                int a = -1;
                int b;
                int c = 0;
                for (int i = 0;i < text.length();i++){
                    if (text.charAt(i) == '('){
                        if (c == 0){
                            a = i;
                        }
                        c++;
                    }
                    if (text.charAt(i) == ')' && c > 0){
                        if (c == 1){
                            c=0;
                            b=i;
                            String gs = text.substring(a,b+1);
                            String bs = text.substring(a+1,b);
                            text = text.replace(gs,new FlexibleFormula(bs).getResult()+"");
                            if (text.contains("(")){
                                text = new FlexibleFormula(text).getResult() + "";
                            }
                        }else {
                            c--;
                        }
                    }
                }
            }
        }else if (text.contains(")")){
            text = text.replace(")","");
        }
        try {
            return Double.parseDouble(text);
        }catch (NumberFormatException ignored){}
        text = text.replace(" ","")
                .replace("--","+")
                .replace("-+","-")
                .replace("+-","-");
        ArrayList<String> list = toArrays();
        while (list.size() > 1){
            if (list.contains("*") || list.contains("/")){
                for (int i = 0;i < list.size();i++){
                    String msg = list.get(i);
                    if (msg.equals("*")){
                        double a = Double.parseDouble(list.get(i - 1));
                        double b = Double.parseDouble(list.get(i + 1));
                        double m = a * b;
                        list.add(i-1, String.valueOf(m));
                        list.remove(i);
                        list.remove(i);
                        list.remove(i);
                        break;
                    }else if (msg.equals("/")){
                        double a = Double.parseDouble(list.get(i - 1));
                        double b = Double.parseDouble(list.get(i + 1));
                        double m = a / b;
                        list.add(i-1, String.valueOf(m));
                        list.remove(i);
                        list.remove(i);
                        list.remove(i);
                        break;
                    }
                }
            }else if (list.contains("+") || list.contains("-")){
                for (int i = 0;i < list.size();i++){
                    String msg = list.get(i);
                    if (msg.equals("+")){
                        double a = Double.parseDouble(list.get(i - 1));
                        double b = Double.parseDouble(list.get(i + 1));
                        double m = a + b;
                        list.add(i-1, String.valueOf(m));
                        list.remove(i);
                        list.remove(i);
                        list.remove(i);
                        break;
                    }else if (msg.equals("-")){
                        double a = Double.parseDouble(list.get(i - 1));
                        double b = Double.parseDouble(list.get(i + 1));
                        double m = a - b;
                        list.add(i-1, String.valueOf(m));
                        list.remove(i);
                        list.remove(i);
                        list.remove(i);
                        break;
                    }
                }
            }
        }
        return Double.parseDouble(list.get(0));
    }

    public ArrayList<String> toArrays(){
        String msg = text;
        ArrayList<String> list = new ArrayList<>();
        String[] args = {"*","/","+","-"};
        while (true){
            String sign = null;
            int i = 99999;
            for (String arg : args){
                int n = msg.indexOf(arg);
                if (n == -1)continue;
                if (n < i){
                    i = n;
                    sign = arg;
                }
            }
            if (sign == null){
                list.add(msg);
                break;
            }else {
                String replace = msg.substring(0,i);
                list.add(replace);
                list.add(sign);
                msg = msg.substring(i+1);
            }
        }
        return list;
    }
}
